package com.sport.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class TokenUtil {

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.expiration}")
    private Long expiration;

    //传入用户登录信息，生成token
    public String generateToken(UserDetails userDetails) {
        Map<String, Object> map = new HashMap<>();
       map.put("username",userDetails.getUsername());
       map.put("created",new Date());
        return this.generateToken(map);
    }

    /**
     * 根据荷载生成token
     * @param
     * @return
     */
//    public String generateToken(Map<String, Object> map) {
//        return Jwts.builder()
//                .setClaims(map)
//                .signWith(SignatureAlgorithm.HS512, secret)
//                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
//                .compact();
//    }
    public String generateToken(Map<String, Object> map) {
        String token=Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.HS512, secret)
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .compact();
        System.out.println("获得的token数据"+token);
        return token;
    }

    /**
     * 从token中获取用户名
     * @param token
     * @return
     */
    public String getUserNameFromToken(String token) {
        System.out.println("getUserNameFromToken中传入的token"+token);
        String username= (String) getTokenBody(token).get("username");
        String s=getTokenBody(token).getSubject() ;
        System.out.println("s的值是"+s);
        System.out.println("getUserNameFromToken"+username);
        return username;
    }

    /**
     * token是否过期
     * @param token
     * @return
     */
    public boolean isExpiration(String token) {
        return getTokenBody(token).getExpiration().before(new Date());
    }

    /**
     * 验证token是否有效
     * @param token
     * @param userDetails
     * @return
     */
    public boolean validateToken(String token, UserDetails userDetails) {
        String username = getUserNameFromToken(token);
        return username.equals(userDetails.getUsername()) && !isExpiration(token);
    }

    /**
     * 判断token是否可以被刷新
     * @param token
     * @return
     */
    public boolean canRefresh(String token) {
        return !isExpiration(token);
    }

    /**
     * 刷新token
     * @param token
     * @return
     */
    public String refreshToken(String token) {
        Claims claims = getTokenBody(token);
        claims.setExpiration(new Date());
        return this.generateToken(claims);
    }

    /**
     * 获取token中的信息
     * @param token
     * @return
     */
    public Claims getTokenBody(String token) {
        try {
            System.out.println("进入getTokenBody");
            Claims claims=Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
            System.out.println("getTokenBody的token数据"+claims);
            return claims;
        } catch (Exception e) {
            return null;
        }

    }

}
